//
//  TableViewDataSource.h
//  TableViewDataSource
//
//  Created by hibo on 2019/6/10.
//  Copyright © 2019 hibo. All rights reserved.
//

#import "TableViewDataSource.h"


@interface TableViewDataSource ()

@property(nonatomic, strong) NSArray* cells;
@property(nonatomic, copy) NSString* identifier;
@property(nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@property(nonatomic, assign) BOOL isTwo;//是否为二维数组

@end

@implementation TableViewDataSource

#pragma mark - source初始化将主要参数引出在主视图中处理数据展示
-(instancetype)initWithDataArr:(NSArray *)dataArr identifier:(NSString *)identifier  configureCellBlock:(TableViewCellConfigureBlock)configureCellBlock {
    self = [super init];
    if (self) {
        self.dataArr = dataArr;
        self.identifier = identifier;
        self.configureCellBlock = configureCellBlock;
    }
    return self;
}
-(void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    self.isTwo = NO;
    if (dataArr.firstObject) {
        if ([dataArr.firstObject isKindOfClass:[NSArray class]]) {
            self.isTwo = YES;
        }
    }
}

#pragma mark - 实现代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.dataArr.count==0) {
        return 0;
    }else{
        return self.isTwo?self.dataArr.count:1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.isTwo?[self.dataArr[section] count]:self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    id model = self.dataArr[indexPath.row];
    self.configureCellBlock(cell, model, indexPath);
    return cell;
}
@end

#pragma mark - 实现头视图、尾视图、头视图高度、尾视图高度的代理方法，将参数引出在主视图中获取
@interface TableViewDelegate ()
@property(nonatomic, copy) TableViewRowHeightBlock rowHeightBlock;
@property(nonatomic, copy) TableViewHeaderHeightBlock headerHeightBlock;
@property(nonatomic, copy) TableViewFooterHeightBlock footerHeightBlock;
@property(nonatomic, copy) TableViewHeaderBlock headerBlock;
@property(nonatomic, copy) TableViewFooterBlock footerBlock;
@end

@implementation TableViewDelegate

//初始化行高、组高、头视图、尾部视图
-(instancetype)initWithRowHeight:(TableViewRowHeightBlock)rowHeightBlock
                    headerHeight:(TableViewHeaderHeightBlock)headerHeightBlock
                    footerHeight:(TableViewFooterHeightBlock)footerHeightBlock
                          header:(TableViewHeaderBlock)headerBlock
                          footer:(TableViewFooterBlock)footerBlock{
    self = [super init];
    if (self) {
        self.rowHeightBlock = rowHeightBlock;
        self.headerHeightBlock = headerHeightBlock;
        self.footerHeightBlock = footerHeightBlock;
        self.headerBlock = headerBlock;
        self.footerBlock = footerBlock;
    }
    return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.rowHeightBlock) {
        return self.rowHeightBlock(indexPath);
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.footerHeightBlock) {
        return self.footerHeightBlock(section);
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.headerBlock) {
        return self.headerBlock(section);
    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.footerBlock) {
        return self.footerBlock(section);
    }
    return nil;
}
@end
